import Vue from 'vue'
import App from './App'
import header from './pages/header/header.vue'
import footer from './pages/footer/footer.vue'

Vue.config.productionTip = false
Vue.component("myheader", header)
Vue.component("myfooter", footer)

Vue.prototype.$checkLogin = function(curRoute) {
	let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
	
	return new Promise((resolve, reject) => {
		uni.getStorage({
			key: 'UserList_h5',
			success: (res) => {
				if (res.data.id != "") {
					const login_time = uni.getStorageSync("login_time_h5");
					const time_now=Date.now();
					var _isexpired=time_now-login_time;
					//console.log(_isexpired)
					//过期时间 86400000 24小时
					if(_isexpired<=86000000){
						resolve(res.data);
					}
					else{
						resolve('false');
						logout();
					}
					

				} else {
					resolve('false');
					if (curRoute != 'pages/login/login' && curRoute != 'pages/Index/index' && curRoute != 'pages/login/register') {
						logout();
					}

				}
			},
			fail: function(e) {
				resolve('false');
				if (curRoute != 'pages/login/login' && curRoute != 'pages/Index/index' && curRoute != 'pages/login/register') {

					logout();
				}

			}
		});

	});
}
Vue.prototype.$logout=function(){
	logout();
}
function logout(){
	uni.removeStorageSync("login_time_h5")
	uni.removeStorageSync("token_h5")
	uni.removeStorage({
		key: 'UserList_h5',
		success: function (res) {
			uni.redirectTo({
				url: '/pages/login/login'
			});
			setTimeout(function() {
				uni.showToast({
					title: "登录已过期",
					icon: "none"
				});
			}, 400)
		}
	});
	
}
App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
